#ifndef BASTERD_ENGINE_UTIL_SYNCHRONIZED_VALUE_CLASS_H_
#define BASTERD_ENGINE_UTIL_SYNCHRONIZED_VALUE_CLASS_H_

#include "Mutex.h"

namespace basterd
{
	namespace util
	{
		/**
		 * Follows ideas from the C++ TR1 and some books.
		 */
		template <typename T>
		class SynchronizedValue
		{
		public:

			/**
			 * Default constructor. You must assign a value later.
			 */
			SynchronizedValue(void)
				: _mutex(), _value()
			{
			}
			/**
			 * Assignment constructor
			 */
			SynchronizedValue(const T& rhs)
				: _mutex(), _value(rhs)
			{
			}

			/**
			 * dummy
			*/
			~SynchronizedValue(void)
			{
			}

			/**
			 * Assignment operator
			 */
			SynchronizedValue& operator =(const T& rhs)
			{
				MutexLocker ml(_mutex);
				_value = rhs;
				return *this;
			}

			/**
			 * Conversion operator
			 */
			operator T(void) const
			{
				::basterd::MutexLocker ml(_mutex);
				return _value;
			}

		protected:

			mutable ::basterd::Mutex _mutex;
			T _value;
		};
	}
}

#endif
